$(function () {

  function loadSwiperList() {
    $.ajax({
      type: 'get',
      url: '/admin/swipers',
      success: function (res) {
        console.log(res);
        if (res.status == 0) {
          let str = ''
          $.each(res.data, function (index, value) {
            let sign = '';
            let Colors = ''
            if (value.swiperstatus == 1) {
              sign = '√';
              Colors = 'green';
            } else if (value.swiperstatus == 2) {
              sign = '×';
              Colors = 'cyan';
            }
            str += `<tr>
                    <td>${value.id}</td>
                    <td>
                        <img src="http://localhost:8888/uploads/${value.swiperimg}">
                    </td>
                    <td>${value.swiperimg}</td>
                    <td>
                        
                        <span data-id='${value.id}' data-status='${value.swiperstatus}' class="layui-badge layui-bg-${Colors}">${sign}</span>
                        
                    </td>
                    <td>
                        <button data-id="${value.id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger delete">
                            删除
                        </button>
                    </td>
                </tr>
        `

          })
          $("tbody").html(str)
        }
        // var tags = template('table-tpl', res)
        // $('.layui-table tbody').html(tags)
      }
    })
  }

  loadSwiperList()




  // *************************************轮播图状态切换  使用还是未使用
  // $('.layui-tablt tbody').on('change', '.layui-badge', function () {
  $('.layui-table tbody').on('click', '.layui-badge', function (e) {
    let a = ''
    if ($(this).attr('data-status') == 1) {
      a = '2';
    } else
      if ($(this).attr('data-status') == 2) {
        a = '1';
      }
    $.ajax({
      type: "put",
      url: `/admin/swipers/${$(e.target).data('id')}`,
      data: {
        status: Number(a)
      },
      success: function (e) {
        console.log(e);
      }
    })

    loadSwiperList();
  })
  // })







  // ***************************************删除操作

  $('.layui-table tbody').on('click', '.delete', function (e) {
    $.ajax({
      type: "/delete",
      url: `admin/swipers/${$(e.target).data('id')}`,
      success: function (e) {
        console.log(e);
      }
    })

    loadSwiperList();
  })






  // ***************************************上传文件

  $('body').on('click', '#uploadSwiper', function () {
    $('#myfile').click()
  })

  // 监听文件选中事件
  $('body').on('change', '#myfile', function (e) {
    let files = e.target.files
    var fd = new FormData()
    $.each(files, function (index, item) {
      fd.append('swipers', item)
    })


    $.ajax({
      type: "post",
      url: "/admin/swipers",
      data:
        fd,
      processData: false,
      contentType: false,
      success: function (res) {
        console.log(res);
      },

    }
    );




  })
})